<?php	
	include "header.html";
	include "mensaje.php";
?>

  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script type="text/javascript" src="js/jquery-ui/jquery.ui.dialog.min.js"></script>  
  <style>
  .tablaDetalle{
  	border: 1px solid black;
  	cellpadding: 0px;
  	cellspacing: 0px;
  }
  .tablaDetalle td, th {
  	border: 1px solid black;
  	cellpadding: 0px;
  	cellspacing: 0px;
  	text-align: center;
  	width: 80px;
  }
  </style>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script type="text/javascript" src="framework/js/util.js"></script>
	<script type="text/javascript" src="framework/js/DB.js"></script>
  <script>  
  var maquinas;
  var estados;
  var tipos;
  var desusoId;
  var cargo=false;
  $(document).ready(function(){
  	jQuery.ajaxSetup({async:false});
  	maquinas=makeCombo("elementoDiv","maquina","maquina","descripcion","");
  	estados=makeCombo("containerEstado","item_estado","matriceria_item_estado","descripcion","");
  	tipos=makeCombo("containerTipo","item_tipo","matriceria_item_tipo","descripcion","generaNumeroItem");
  	tipoItems= DBHloadMap("matriceria_item_tipo", {}, "matriz");
	var paramsDe={};
	paramsDe["numero"]=9999; 
	matriceriaDesuso=load("matriceria",paramsDe);
	desusoId=matriceriaDesuso.rows[0].id;
  	armaVista(desusoId,null,null);
	cargo=true;
	$( "#tabs" ).tabs();
  });

function filtraDesuso(){
	var min = $("#filtroMin").val();
	var max = $("#filtroMax").val();
	if (max=="" && min==""){
		armaVista(desusoId,null,null);		
	}else{
		armaVista(desusoId,min,max);
	}
}
  

  function armaVista(id,min,max){
	 	var x;
		var strM="";
		var strA="";
		var params={};
		params["idMatriceria"]=id;		
		items=DBHload("matriceria_item",params,"idTipo");
		
		
		for (x=0;x<items.rowCount;x++){
			var obj=items.rows[x];		
			str="";
			str += "<h3>" +  tipos[obj.idTipo] + " (" +  obj.codigo + ") | <span style='text-indent: 5em'>" + estados[obj.idEstado] + " </span> " +  "</h3>";
			if (obj.planoURL!=""){
				str+="<div><p><span style='display:block'><a href=\"file://///" + obj.planoURL + "\" target='_blank'>Ver Plano .. </a></span>";
			}else{
				str+="<div><p><span style='display:block;color: red;font-weight:bold;'>Item sin Plano Adjunto !!</span>";
			}
			
			var params={};
			params["idItem"]=obj.id;
			if (min!=null && min!=""){
				params["minimo"]=["GE",min];
			}
			if (max!=null && max!=""){
				params["maximo"]=["LE",max];
			}
			cotas=DBHload("matriceria_item_cota",params,"nombre");
			if (cotas.rowCount>0){
				str += "<table class='tablaDetalle'><thead><th>Cota</th><th>Minimo</th><th>Maximo</th></thead>";
				for (i=0;i<cotas.rowCount;i++){				
					var cota=cotas.rows[i];
					str+= "<TR><TD>" + cota.nombre + "</TD><TD>" + cota.minimo + "</TD><TD>" + cota.maximo + "</TD></TR>";
				}
				str+="</table></p>";								
				str+="<span style=''><div style='display:block'>Nro Matriceria Destino: <input type='text' size=5 id='nroMatriceria'/> <button class='btn btn-smaller' type='button'  onclick='asocia(" + obj.id + ")'>Reutilizar... </button></div>";
				str+="<div style='display:none;color:red;font-size:13px;' id='errorMatriceriaDestino'>Matriceria Destino Invalida!</div></span>";
				str+="</div>";
			}else{
				str="";
			}
			if (tipoItems[obj.idTipo]==1){
				strM+=str;
			}else{
				strA+=str;
			}
		}
		$("#vistaA").html(strA);			
		$("#vistaM").html(strM);
		if (cargo){
			$( "#vistaA" ).accordion('destroy').accordion();
			$( "#vistaM" ).accordion('destroy').accordion();
		}else{
			$( "#vistaA" ).accordion();
			$( "#vistaM" ).accordion();
		}
		bloqueaLetrasNew("nroMatriceria");
		
	}

	function asocia(id){				
		obj=DBHloadById("matriceria_item", id)
		var destino = $("#nroMatriceria").val();
		if (destino==""){
			$("#errorMatriceriaDestino").slideDown(400).delay(3000).slideUp(400);
		}else{
			var params={};
			params["numero"]=destino;		
			mats=DBHload("matriceria",params);
			if (mats.rowCount>0){
				var idDestino=mats.rows[0].id;
				var codigo=generaNumeroItem(mats.rows[0], obj);
				var paramsC={};
				var fieldsC={};
				paramsC["id"]=obj.id;
				fieldsC["codigo"]=codigo;
				fieldsC["idEstado"]=1; // EN USO
				fieldsC["idMatriceria"]=idDestino;					
				DBHupdateByFields("matriceria_item",fieldsC,paramsC);
				alert("Matriz reasignada a Matriceria Nro: " + destino);
				armaVista(desusoId,null,null);	
			}else{
				$("#errorMatriceriaDestino").slideDown(400).delay(3000).slideUp(400);
				$("#nroMatriceria").val("");
			}
		}
	}

	function generaNumeroItem(mat,obj){
		var params={};
		params["id"]=obj.idTipo;;
		var row=load("matriceria_item_tipo",params);
		var prefix=row.rows[0].prefijo;
		var params={};
		params["idMatriceria"]=mat.id;
		params["idTipo"]=row.rows[0].id;
		items=count("matriceria_item",params);
		var numero;
		if ((items+1)<10){
			numero="00"+(items+1);
		}else if((items+1)>=10 && items<100){
			numero="0"+(items+1);
		}else{
			numero=(items+1);
		}
		return mat.numero + "-" + prefix + numero;	
	}
  
  </script>
</head>
<?php 
	include "menuMatriceria.html";
?>
<div style="width: 100%;">
<div class="grid_10">
	<div class="box round">
		<h2>Matrceria en Desuso</h2>
		<div class="block">
			<span style="display:block; font-size: 15px; font-weight: bold;padding-bottom: 10px;">Filtro de Matrices por Min y Max de Cota </span> 
			<div style="padding-bottom: 20px;"><span>Minimo: </span> <input type="text" id="filtroMin" onkeyup="filtraDesuso()" size="5"> <span>Maximo: </span> <input type="text" id="filtroMax" onkeyup="filtraDesuso()" size="5" ></div>
			<br>
			<div id="tabs">
				<ul>
					<li><a href="#vistaM">Matrices</a></li>
					<li><a href="#vistaA">Auxiliares</a></li>
				</ul>
				<div id="vistaM"></div>
				<div id="vistaA"></div>
			</div>
		</div>
	</div>
</div>

</div>
<?php include "footer.html"?>